<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use think\Cookie;

class Pay extends Backend{
    public function __construct()
    {
        parent::__construct();

        $this->PayModel=model('User.Pay');
        $this->RecordModel=model('User.Record');
        $this->UserModel=model('User.User');
    }

    public function index(){
        $paylist=$this->PayModel->with(['users','admins'])->paginate(10);

        $count=$this->PayModel->count();

        $pagestr=$paylist->render();

        $this->assign([
            'paylist'=>$paylist,
            'count'=>$count,
            'pagestr'=>$pagestr,
        ]);

        return $this->fetch();
    }

    public function pass()
    {
        $success = [
            'result' => false,
            'msg' => ''
        ];

        if($this->request->isAjax())
        {
            
            $ids = $this->request->param('ids',0);
            
            $pay = $this->PayModel->find($ids);

            $this->PayModel->startTrans();
            $this->RecordModel->startTrans();

            $admin=Cookie::get('LoginUser');

            $Data=[
                'id'=>$ids,
                'status'=>'1',
                'adminid'=>$admin['id'],
            ];

            $Status=$this->PayModel->isUpdate(true)->save($Data);

            if($Status === FALSE){
                $success['result'] = false;
                $success['msg'] = $this->PayModel->getError();
                return json($success);
            }
            
            $RecordData=[
                'point'=>$pay['point'],
                'state'=>3,
                'userid'=>$pay['userid']
            ];
            $RecordData['content']="【充值积分：<b>".$pay['point']."积分</b>】 - 充值时间：".$pay['createtime'];

            $RecordStatus=$this->RecordModel->save($RecordData);

            if($RecordStatus===FALSE){
                $this->PayModel->rollback();
                $success['result'] = false;
                $success['msg'] = $this->RecordModel->getError();
                return json($success);
            }

            if($Status===FALSE || $RecordStatus===FALSE){
                $this->RecordModel->rollback();
                $this->PayModel->rollback();
                $success['result'] = false;
                $success['msg'] = '审核通过失败';
                return json($success);
            }else{
                $this->RecordModel->commit();
                $this->PayModel->commit();
                $success['result'] = true;
                $success['msg'] = '审核通过成功';
                return json($success);
            }
        }
    }

    public function onpass()
    {
        $success = [
            'result' => false,
            'msg' => ''
        ];

        if($this->request->isAjax())
        {
            
            $ids = $this->request->param('ids',0);
            
            $pay = $this->PayModel->find($ids);

            $this->PayModel->startTrans();
            $this->UserModel->startTrans();
            $this->RecordModel->startTrans();

            $Data=[
                'id'=>$ids,
                'status'=>'2'
            ];

            $Status=$this->PayModel->isUpdate(true)->save($Data);

            if($Status === FALSE){
                $success['result'] = false;
                $success['msg'] = $this->PayModel->getError();
                return json($success);
            }

            $User=$this->UserModel->where(['id'=>$pay['userid']])->find();

            $point=bcsub($User['point'],$pay['point']);

            $UserData=[
                'id'=>$User['id'],
                'point'=>$point
            ];

            $UserStatus=$this->UserModel->isUpdate(true)->save($UserData);

            if($UserStatus===FALSE){
                $this->PayModel->rollback();
                $success['result'] = false;
                $success['msg'] = $this->UserModel->getError();
                return json($success);
            }
            
            $RecordData=[
                'point'=>$pay['point'],
                'state'=>3,
                'userid'=>$pay['userid']
            ];
            $RecordData['content']="【充值积分失败：<b>-".$pay['point']."积分</b>】 - 充值时间：".$pay['createtime'];

            $RecordStatus=$this->RecordModel->save($RecordData);

            if($RecordStatus===FALSE){
                $this->UserModel->rollback();
                $this->PayModel->rollback();
                $success['result'] = false;
                $success['msg'] = $this->RecordModel->getError();
                return json($success);
            }

            if($Status===FALSE || $RecordStatus===FALSE || $UserStatus===FALSE){
                $this->RecordModel->rollback();
                $this->UserModel->rollback();
                $this->PayModel->rollback();
                $success['result'] = false;
                $success['msg'] = '审核通过失败';
                return json($success);
            }else{
                $this->RecordModel->commit();
                $this->UserModel->commit();
                $this->PayModel->commit();
                $success['result'] = true;
                $success['msg'] = '审核通过成功';
                return json($success);
            }
        }
    }
}